import requests

# 文献库检索接口的基础URL
BASE_URL = "http://180.184.65.98:38880/atomgit"

def search_papers(query, top_k=5):
    """
    You are a news search specialist. Your task is to:
    1. Search for the most relevant and recent news on the given topic
    2. Ensure the results are from reputable sources
    3. Return the raw search results in a structured format
    根据文本查询搜索论文
    :param query: 查询文本
    :param top_k: 返回的论文数量，默认为5
    :return: 返回匹配的论文信息（JSON格式）
    """
    url = f"{BASE_URL}/search_papers"
    params = {"query": query, "top_k": top_k}
    response = requests.get(url, params=params)
    if response.status_code == 200:
        return response.json()
    else:
        raise Exception(f"检索失败，状态码：{response.status_code}")

def query_by_paper_id(paper_id, top_k=5):
    """
    根据论文ID查询论文
    :param paper_id: 论文ID
    :param top_k: 返回的论文数量，默认为5
    :return: 返回匹配的论文信息（JSON格式）
    """
    url = f"{BASE_URL}/query_by_paper_id"
    params = {"paper_id": paper_id, "top_k": top_k}
    response = requests.get(url, params=params)
    if response.status_code == 200:
        return response.json()
    else:
        raise Exception(f"检索失败，状态码：{response.status_code}")

def query_by_title(title, top_k=5):
    """
    根据论文标题查询论文
    :param title: 论文标题
    :param top_k: 返回的论文数量，默认为5
    :return: 返回匹配的论文信息（JSON格式）
    """
    url = f"{BASE_URL}/query_by_title"
    params = {"title": title, "top_k": top_k}
    response = requests.get(url, params=params)
    if response.status_code == 200:
        return response.json()
    else:
        raise Exception(f"检索失败，状态码：{response.status_code}")
def query_paper_metadata_that_title_contain(title, top_k=100):
    """
    搜索标题中包含特定关键词的论文元数据 (ID和标题)
    :param title: 要搜索的标题关键词
    :param top_k: 返回论文元数据的数量，默认为100
    :return: 论文ID和标题列表，每个元素是一个元组 (paper_id, title)
    """
    url = f"{BASE_URL}/query_paper_metadata_that_title_contain"
    params = {"title": title, "top_k": top_k}
    response = requests.get(url, params=params)
    if response.status_code == 200:
        return response.json()
    else:
        raise Exception(f"检索失败，状态码：{response.status_code}")

def titles_like(title, top_k=100):
    """
    查找与输入标题相似的标题列表
    :param title: 要查找相似标题的输入标题
    :param top_k: 返回最相似的标题数量，默认为100
    :return: 相似标题字符串列表
    """
    url = f"{BASE_URL}/titles_like"
    params = {"title": title, "top_k": top_k}
    response = requests.get(url, params=params)
    if response.status_code == 200:
        return response.json()
    else:
        raise Exception(f"检索失败，状态码：{response.status_code}")